# Maintainer: Giovanni Scafora <giovanni@archlinux.org>
# Maintainer: Zach Bacon <11doctorwhocanada@gmail.com>
# Contributor: Gilles CHAUVIN <gcnweb@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=live-media
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2022.04.26
pkgrel=2
pkgdesc="A set of C++ libraries for multimedia streaming (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:LGPL-3.0-or-later')
url="http://live555.com/liveMedia"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "rsync")
source=("https://download.videolan.org/contrib/live555/live.${pkgver}.tar.gz"
        0001-Add-a-pkg-config-file-for-the-shared-libraries.patch
        0002-expose-server_string.patch
        0003-mingw-static-libs.patch
        0004-live555-formatmessage.patch
        0005-Fix-testProgs-static-lib-link-order.patch
        0006-mingw-NO_GETIFADDRS.patch)
sha256sums=('24fd24d53da1ac8f8282173039e99c9952186c18404b1fc3b1d4c0e9f49414e7'
            '8611cb5eb31571d4026a66d47a98f2a08abf3b6c3ac373ee0c28638a3bf3c4dc'
            'ae6303f91d09ebc89ff622db9b622567fb98728630b1639786d62dcb1cea9dce'
            '6111b8119b80eca6a2fdd1c116f2e3509d62098d3c36944bd1bf83bb03342c87'
            '58255bbd2e43112e36647d1194aa0cfa98d0e9bf58a8a41d59e1e81e1589028a'
            '9fb73d9da3c9448cc47ef14a3978861ed0d442822f48481ae8d858db1b04ae8e'
            '1a52a453eff64204d06f7616da19ce8def5798194ed20e93cc470cd0007a35ba')

prepare() {
  cd ${srcdir}/live

  chmod -R a+w *
  find . -name "*.rej" -exec rm -f {} \;
  find . -name "*.orig" -exec rm -f {} \;

  [[ -f live555.pc.in ]] && rm -f live555.pc.in
  patch -p1 -i ${srcdir}/0001-Add-a-pkg-config-file-for-the-shared-libraries.patch
  patch -p1 -i ${srcdir}/0002-expose-server_string.patch
  patch -p1 -i ${srcdir}/0003-mingw-static-libs.patch
  patch -p1 -i ${srcdir}/0004-live555-formatmessage.patch
  patch -p1 -i ${srcdir}/0005-Fix-testProgs-static-lib-link-order.patch
  patch -p1 -i ${srcdir}/0006-mingw-NO_GETIFADDRS.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}
  # This cp is necessary since these are symlink targets but get extracted
  # later. While this shouldn't matter (symlink targets are allowed not to
  # exist on Windows as per Unix), it currently does as various MSYS2 tools
  # pre-date working Windows symlinks.
  cp ${srcdir}/live/COPYING* .
  rsync -rav ${srcdir}/live/* .
  ./genMakefiles mingw

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  make install DESTDIR=${pkgdir} PREFIX=${MINGW_PREFIX}
  make install_shared_libraries DESTDIR=${pkgdir} PREFIX=${MINGW_PREFIX}

  for dir in BasicUsageEnvironment groupsock liveMedia UsageEnvironment; do
    install -dm755 ${pkgdir}${MINGW_PREFIX}/{bin,lib,include/${dir}}
    install -m644 ${dir}/*.a "${pkgdir}${MINGW_PREFIX}/lib"
    install -m644 ${dir}/include/*.h* "${pkgdir}${MINGW_PREFIX}/include/${dir}"
  done

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/pkgconfig"
  install -m644 ${pkgdir}/pkgconfig/live555.pc "${pkgdir}${MINGW_PREFIX}/share/pkgconfig"
  sed -i "s|^prefix=$|prefix=${MINGW_PREFIX}|" "${pkgdir}${MINGW_PREFIX}/share/pkgconfig/live555.pc"
  sed -i "s|^libdir=$|libdir=${MINGW_PREFIX}/lib|" "${pkgdir}${MINGW_PREFIX}/share/pkgconfig/live555.pc"
  rm -rf ${pkgdir}/pkgconfig

  for testprog in `find testProgs -type f -perm 755`; do
    install ${testprog} "${pkgdir}${MINGW_PREFIX}/bin"
  done
}
